var customer = {};

customer.resultFunction = null;

customer.selectCustomer = function(isSupplierOrCustomer,resultFunction){
	customer.resultFunction = resultFunction;
	var url = basePath + "/admin/customer/selectcustomer.do";
	var dlg = $("<div/>").dialog({
		height:500,
		width:800,
		title:'选择往来单位',
		iconCls:'icon',
		loadUrl:url,
		buttons:[
		         {id:'_sel_customer_btn',text:'选 择',type:'button',iconCls:'icon-ok',handler:function(){
		        	var selrow = dlg.find("#select_customer_data").jqGrid("getGridParam","selrow");
		        	if(!selrow){
		        		$.alert("提示","没有选择往来单位信息!",300,140,"info");
		        		return;
		        	}
		        	var data = dlg.find("#select_customer_data").jqGrid("getRowData",selrow);
		        	customer.resultFunction(data);
		        	dlg.dialog("destroy");
		         }},
		         {text:'关 闭',type:'button',iconCls:'icon-cancel',handler:function(){
		        	 dlg.dialog("destroy");
		         }},
		         ],
		onLoad:function(){
			customer.setCustomerClassTree(dlg,isSupplierOrCustomer);
			customer.setDataGrid(dlg,isSupplierOrCustomer);
			dlg.find("#search-btn").button({iconCls:'icon-search',handler:function(){
				var name = dlg.find("input[name='name']").val().trim();
				var mnemonic = dlg.find("input[name='mnemonic']").val().trim();
				var postData = dlg.find("#select_customer_data").jqGrid("getGridParam","postData");
				postData.name = name,
				postData.mnemonic = mnemonic;
				dlg.find("#select_customer_data").jqGrid("setGridParam",{"postData":postData}).trigger('reloadGrid');
			}});
			dlg.find("#search-clear").button({iconCls:'icon-cancel',handler:function(){
				dlg.find("input[name='name']").val("");
				dlg.find("input[name='mnemonic']").val("");
				dlg.find("#search-btn").trigger("click");
			}});
		}
	});
	dlg.dialog("loadPage");
}


customer.setDataGrid = function(dialog,isSupplierOrCustomer){
	var url = basePath + "admin/customer/list.do";
	
	var postData = null;
	if(isSupplierOrCustomer == "supplier"){
		postData = {isSupplier:true,status:'normal',classId:0};
	}else{
		postData = {isCustomer:true,status:'normal',classId:0};
	}
	var colNames = ['id','单位名称','所属类别', '来源','电话','地址','邮政编码'];
	var colModel = [
	                {name:"id",hidden:true},
	                {name:'name',index:'name', width:150,align:'left'},
				    {name:'customerClass.name',index:'className', width:80,align:'left',sortable:false},							 
					{name:'source.name',index:'sourceName', width:80,align:'left',sortable:false}, 
					{name:'telPhone',index:'telPhone',width:80,align:'left',sortable:true},
					{name:'address',index:'address',width:120,align:'left'},
					{name:'post',index:'post',width:80,align:'left'}
				];
	dialog.find("#select_customer_data").jqGrid({
		url:url,
		datatype: "json",
		mtype: "POST",
		postData:postData,
		height:378,
		multiselect:false,
		autowidth:true,
		autoencode:true,
		viewrecords:true,
		forceFit:true,
		gridview:true,
		loadui:'block',
		rowNum:0,
		sortorder:'asc',
		rownumbers:true,
		colNames:colNames,
		colModel:colModel,
		jsonReader:{
			id:'id',
			root:'result.data',
			page:'result.page.curPageNo',
			total:'result.page.totalPage',
			repeatitems:false
		},
		prmNames:{
			page:'page.curPageNo',
			rows:'page.pageSize',
			sort:'orderField',
			order:'orderType'		
		},
		page:1,
		pagerpos:'left',
		pager:dialog.find("#select_customer_page"),
		ondblClickRow:function(){
			dialog.find("#_sel_customer_btn").trigger("click");
		}
	});
}

customer.setCustomerClassTree = function(dialog,isSupplierOrCustomer){
	var root = [{
		id:"0",
		name:'往来单位分类信息',			
		isParent:true,
		subClasses:true,
		open:true
	}];
	dialog.find("#customerClassTree").treeview({
		treeNodeKey:'id',
		treeNodeParentKey:'parentId',
		hasChildKey:'subClasses',
		nameCol:'name',
		expandSpeed:"",
		async:true,
		asyncUrl:function(treeNode){
			return basePath + "admin/customerclass/list.do?parentId=" + treeNode.id;	
		},
		root:{isRoot:true,nodes:root},
		callback:{
			beforeAsyncSuccess:function(msg){
				if(msg.message.code == '-2'){
					$.alert('提示','你还没有登录或Session已经过期，请重新登录系统!',300,170,'info');
					return false;
				}
				if(msg.message.code == "-1"){
					$.alert("提示","获取信息失败，原因：" + msg.message.mess,300,170,'error');
					return false;
				}
				return true;
			},
			click:function(event,treeId,treeNode){
				var url = dialog.find("#select_customer_data").jqGrid("getGridParam","url");
				var postData = null;
				if(isSupplierOrCustomer == "supplier"){
					postData = {isSupplier:true,status:'normal',classId:treeNode.id};
				}else{
					postData = {isCustomer:true,status:'normal',classId:treeNode.id};
				}
				dialog.find("#select_customer_data").jqGrid("setGridParam",{"url":url,"postData":postData}).trigger('reloadGrid');
			}
		}
	});
}
